function a(ele){return document.querySelector(ele)}
function c(ele){return document.createElement(ele)}

//ajax_get
function ajax_get(url){
    return new Promise((resolve,reject)=>{
        let xhr=new XMLHttpRequest()
        xhr.open("get",url,true)
        xhr.send()
        xhr.onreadystatechange=()=>{
            if(xhr.readyState === 4 && xhr.status === 200){
                resolve(xhr.responseText)
            }
        }
    })
}
//ajax_post
function ajax_post(url,str){
    return new Promise((resolve,reject)=>{
        let xhr=new XMLHttpRequest()
        xhr.open("post",url,true)
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
        xhr.send(str)
        xhr.onreadystatechange=()=>{
            if(xhr.readyState === 4 && xhr.status === 200){
                resolve(xhr.responseText)
            }
        }
    })
}

window.onload=function(){
    let url_str=window.location.href.split("?")[1]
    let id=Number(url_str.split("=")[1])
    create(id)
}

function create(id){
    a("article").innerHTML=""
    let arr=JSON.parse(localStorage.getItem("local_content"))
    let newArr=[]
    newArr=arr.filter((item)=>{
        return item.id == id
    })
    $(".swiper-a img").attr("src","./img/"+id+".jpg")
    let content_box=$("<div></div>")
    content_box.addClass("content_box")
    let product_price=$("<div></div>")
    product_price.addClass("product_price").text("￥"+newArr[0].product_price)
    let product_name=$("<div></div>")
    product_name.addClass("product_name").text(newArr[0].product_name)
    let product_size=$("<div></div>")
    product_size.addClass("product_size").text(newArr[0].product_size)
    let tap=$("<div></div>")
    tap.addClass("tap").text("免运费")
    let help=$("<div></div>")
    help.addClass("help").text("帮我选")
    content_box.append($(product_price)).append($(product_name)).append($(product_size)).append($(tap)).append($(help))
    $("article").append($(content_box))
}

$(".a2").on("click",function(){
    let url_str=window.location.href.split("?")[1]
    let id=url_str.split("=")[1]
    let a=JSON.parse(localStorage.getItem("local_collect"))
    let content_collect={
        shop_cart:id
    }
    if(a.length <= 0){
        a.push(content_collect)
    }else{
        if(!a.find((item)=>{return item.shop_cart == content_collect.shop_cart})){
            a.push(content_collect)
        }
    }
    localStorage.setItem("local_collect",JSON.stringify(a))
    $(this).attr("href","http://localhost/mogujie/shop-cart.html?reload")
})
